"""
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License. 
"""

import zope.interface

from meercat.harvester.base.interfaces import IHarvester, IQueriableHarvester



class MockHarvester(object):
    """ A mocked harvester for testing purposes """
    zope.interface.implements(IHarvester)
    
    def __init__(self, instances, source='mockHarvester'):
        self._instances = instances
        self.setSource(source)
    
    def harvest(self):
        """This method should return an iterable that iterates over a collection of
        objects that provide the meercat.interfaces.IInstance interface.
        """
        return self._instances
      
    def getSource(self):
        """Return the name of the source that this harvester is harvesting from"""
        self._source
        
      
    def setSource(self, name):
        """Set the name of the source that this harvester is harvesting from"""
        self._source = name

class MockQueriableHarvester(MockHarvester):
    """ A mocked queriable harvester for testing purposes """
    zope.interface.implements(IQueriableHarvester)
    
    def __init__(self, instances, source='mockQueriableHarvester'):
        super(MockQueriableHarvester, self).__init__(instance, source)
        
        self._ids = {}
        
        for i in self._instances:
            self._ids[i.getIdentifier()] = i
    
    def harvest(self, limit=None, offset=None, from_date=None, until_date=None):
        """This method should return an iterable that iterates over a collection of
        objects that provide the meercat.interfaces.IInstance interface.
        
        Keyword arguments:
        limit -- Place a restriction on the number of results returned (default None)
        offset -- Start the result set at offset into the full result set (default None)
        from_date -- Restrict results to those that have been added/modified since 
          the provided datetime.datetime object. (default None)
        until_date -- Restrict results to those that were added/modified before the
          provided datetime.datetime object. (default None) """
        
        if from_date and until_date:
            data = filter(lambda x: from_date <= x.getLastModified() <= until_date,
                          self._instances)
        elif from_date:
            data = filter(lambda x: from_date <= x.getLastModified(),
                          self._instances)
        elif until_date:
            data = filter(lambda x: x.getLastModified() <= until_date,
                          self._instances)
        else:
            data = self._instances
        
        if limit and offset:
            return data[offset:limit+offset]
        elif limit:
            return data[:limit]
        elif offset:
            return data[offset:]
        
        return data
    
    def get(self, identifier):
        """This method should return a single object that provides the 
        meercat.interfaces.IInstance interface. The resulting objects getIdentifier
        method should return the provided identifier. If the source does not have
        the supplied identifier, a LookupError exception should be raised. """
        return self._ids[identifier]
    
    def count(self):
        """ Return the total number of resources that can be harvested """
        return len(self._instances)